<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}模型`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="参数选择" prop="entrain">
        <el-select v-model="drawerProps.row!.paramName" placeholder="请选择" clearable>
          <el-option v-for="item in entrain" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="实际值" prop="actualValue">
        <el-input v-model="drawerProps.row!.actualValue" placeholder="实际值" clearable></el-input>
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="ModelDrawer">
import { ref, reactive } from "vue";
import { entrain } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import {Model, ModelInstanceParam} from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import {getDModelList, getParamList, queryDataset} from "@/api/modules/new2";

const rules = reactive({
  modelname: [{ required: true, message: "请填写模型名称" }],
  isTrainable: [{ required: true, message: "请选择是否" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<ModelInstanceParam.updateModelInstanceParamList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}
const entrain = ref([]);
const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  const param={
    pageNum: 1,
    pageSize: 100

  }
  const data =await getParamList(param);
  const list= data.data.list;
  entrain.value = list.map((item: any) => ({
    label: item.paramName,
    value: item.paramId
  }));
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}模型成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
